//
//  IWantToTakeTheClothesTableViewCell.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/10.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "IWantToTakeTheClothesTableViewCell.h"

@interface IWantToTakeTheClothesTableViewCell ()

Strong UIImageView *titleImageView;
Strong UILabel *titleLabel;
Strong UILabel *codeLabel;
Strong UILabel *timeLabel;

@end

@implementation IWantToTakeTheClothesTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    self = [super initWithCellIdentifier:cellID];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _titleImageView = [UIImageView new];
        _titleImageView.contentMode = UIViewContentModeScaleAspectFit;
        _titleImageView.image = [UIImage imageNamed:@"root_message_left"];
        
        _titleLabel  = [[UILabel alloc]init];
        _titleLabel.font = DEF_FontSize_14;
        _titleLabel.textColor = getColor(textColor);
        _titleLabel.numberOfLines = 2;
        
        _codeLabel  = [[UILabel alloc]init];
        _codeLabel.font = DEF_FontSize_14;
        _codeLabel.textColor = getColor(textColor);
        //_codeLabel.text = @"取件码【23411】";
        
        _timeLabel  = [[UILabel alloc]init];
        _timeLabel.font = DEF_FontSize_14;
        _timeLabel.textColor = getColor(mainColor);
        
        [self.contentView sd_addSubviews:@[_titleLabel,_titleImageView,_codeLabel,_timeLabel]];
        
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(77 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(27 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
        make.right.equalTo(weakSelf).offset(-10 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(57 / WIDTH_5S_SCALE);
    }];
    
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLabel);
        make.top.equalTo(weakSelf.titleLabel.mas_bottom);
        make.height.mas_equalTo(20 / WIDTH_5S_SCALE);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo( - 10 / WIDTH_5S_SCALE);
        make.centerY.equalTo(weakSelf.codeLabel);
        make.height.mas_equalTo(weakSelf.codeLabel);
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    NSString *strAddress = [NSString stringWithFormat:@"您有一个快递包裹至%@，请您尽快取件!",model[@"address"]];
    NSString *strCode = [NSString stringWithFormat:@"取件码【%@】",model[@"pickup_password"]];
    self.titleLabel.attributedText = [self getAttributeName:strAddress];
    self.codeLabel.attributedText = [self getAttributeName:strCode];
    self.timeLabel.text = [self getStringFromDateString:[NSString stringWithFormat:@"%@",model[@"order_time"]]];
}


- (NSAttributedString *)getAttributeName:(NSString *)name{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

//时间戳转换为时间
- (NSString *)getStringFromDateString:(NSString *)dateString{
    NSString *timeStampString  = dateString;
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dString       = [formatter stringFromDate: date];
    return dString;
}

@end
